<template name="AdminDashboard">
	{{#each adminWidgets}}
	{{> UI.dynamic template=template data=data}}
	{{else}}
	{{> adminDefaultWidgets}}
	{{/each}}
</template>

<template name="AdminDashboardNew">
	{{> adminAlert}}
	{{#if adminTemplate admin_collection_name 'new'}}
		{{#with adminTemplate admin_collection_name 'new'}}
		{{> UI.dynamic template=name data=data }}
		{{/with}}
	{{else}}
		{{> quickForm id="admin_insert" collection=admin_collection fields=admin_fields omitFields=admin_omit_fields buttonContent='Create'}}
	{{/if}}
</template>

<template name="AdminDashboardEdit">
	{{> adminAlert}}
	{{#if adminTemplate admin_collection_name 'edit'}}
		{{#with adminTemplate admin_collection_name 'edit'}}
		{{> UI.dynamic template=name data=data }}
		{{/with}}
	{{else}}
		{{#if admin_current_doc}}
		{{> quickForm id="admin_update" collection=admin_collection doc=admin_current_doc fields=admin_fields omitFields=admin_omit_fields buttonContent='Update'}}
		{{/if}}
	{{/if}}
</template>

<template name="AdminDashboardViewWrapper">
	<div></div>
	{{! This template is used to manually re-render AdminDashboardView on route transition }}
</template>

<template name="AdminDashboardView">
	{{> adminAlert}}
	{{#if adminTemplate admin_collection_name 'view'}}
		{{#with adminTemplate admin_collection_name 'view'}}
		{{> UI.dynamic template=name data=data }}
		{{/with}}
	{{else}}
		{{#if hasDocuments}}
			{{> tabular table=admin_table class="table dataTable"}}
		{{else}}
			<div class="alert alert-info">
				<p>There are no visible items in this collection.</p>
				<p><a href="{{pathFor 'adminDashboardNew' collection=admin_collection_name}}" class="btn btn-primary"><i class="fa fa-plus"></i> {{_ "Add one"}}</a></p>
			</div>
		{{/if}}
	{{/if}}
</template>

<template name="AdminDashboardUsersNew">
	{{> adminAlert}}
	{{# autoForm id="adminNewUser" schema=AdminSchemas.newUser type="method" meteormethod="adminNewUser"}}

	{{>afQuickField name="email"}}
	{{>afQuickField name="chooseOwnPassword"}}

	{{#if afFieldValueIs name="chooseOwnPassword" value=false}}

	{{>afQuickField name="password"}}
	{{>afQuickField name="sendPassword"}}

	{{/if}}

	<button type="submit" class="btn btn-primary">{{_ "Add User"}}</button>

	{{/autoForm}}
	
</template>

<template name="AdminDashboardUsersEdit">
	{{> adminAlert}}
	{{#if adminGetUserSchema}}
	{{> quickForm id="adminUpdateUser" buttonContent="Update" buttonClasses="btn btn-primary btn-sm" collection=adminGetUsers schema=adminGetUserSchema doc=admin_current_doc omitFields="roles,services"}}
	<hr/>
	{{/if}}
	
	<h4>{{_ "User Roles"}}</h4>
	{{#each roles}}
	<button class="btn btn-primary btn-xs btn-remove-role" role="{{this}}" user="{{admin_current_id}}">{{this}}</button>
	{{/each}}
	{{#each otherRoles}}
	<button class="btn btn-default btn-xs btn-add-role" role="{{this}}" user="{{admin_current_id}}">{{this}}</button>
	{{/each}}
	{{#if roles}}
	<p style="margin-top:5px;">{{_ "Click a role to toggle it."}}</p>
	{{else}}
	<p>{{_ "User not in any roles. Click a role to add it to a user."}}</p>
	{{/if}}

	<hr/>
	<h4>{{_ "Reset Password"}}</h4>
	{{#autoForm id="adminSendResetPasswordEmail" schema=AdminSchemas.sendResetPasswordEmail type="method" meteormethod="adminSendResetPasswordEmail"}}
	<div class="form-group hidden">
		<label class="control-label" for="title">ID</label>
		<input value="{{admin_current_id}}" type="text" name="_id" omitfields="createdAtupdatedAt" required="" data-schema-key="_id" class="form-control" autocomplete="off" />
		<span class="help-block"></span>
	</div>
	<p>{{_ "Send a reset password email to"}} {{admin_current_doc.emails.[0].address}}</p>
	<button type="submit" class="btn btn-primary btn-sm">{{_ "Send Email"}}</button>
	{{/autoForm}}

	<hr/>
	<h4>{{_ "Change Password"}}</h4>
	{{#autoForm id="adminChangePassword" schema=AdminSchemas.changePassword type="method" meteormethod="adminChangePassword"}}
	<div class="form-group hidden">
		<label class="control-label" for="title">ID</label>
		<input value="{{admin_current_id}}" type="text" name="_id" omitfields="createdAtupdatedAt" required="" data-schema-key="_id" class="form-control" autocomplete="off" />
		<span class="help-block"></span>
	</div>
	{{>afQuickField name="password"}}
	<button type="submit" class="btn btn-primary btn-sm">{{_ "Change Password"}}</button>
	{{/autoForm}}
</template>

<template name="adminAlert">
	{{#if $.Session.get 'adminSuccess'}}
	<div class="alert alert-success admin-alert">
		{{$.Session.get 'adminSuccess'}}
	</div>
	{{/if}}

	{{#if $.Session.get 'adminError'}}
	<div class="alert alert-danger admin-alert">
		{{$.Session.get 'adminError'}}
	</div>
	{{/if}}
</template>

<template name="adminUsersIsAdmin">
	{{#if adminIsUserInRole this._id 'admin'}}<i class="fa fa-check"></i>{{/if}}
</template>

<template name="adminUsersMailBtn">
	<a href="mailto:{{adminUserEmail this}}" class="btn btn-default btn-xs"><i class="fa fa-envelope"></i></a>
</template>

<template name="adminEditBtn">
	<a href="{{pathFor 'adminDashboardEdit' _id=_id collection=admin_collection_name}}" class="hidden-xs btn btn-xs btn-primary"><i class="fa fa-pencil"></i></a>
	<a href="{{pathFor 'adminDashboardEdit' _id=_id collection=admin_collection_name}}" class="visible-xs btn btn-sm btn-primary"><i class="fa fa-pencil"></i>{{_ "Edit"}}</a>
</template>

<template name="adminDeleteBtn">
	<a data-toggle="modal" doc="{{_id}}" href='#admin-delete-modal' class="hidden-xs btn btn-xs btn-danger btn-delete"><i class="fa fa-times" doc="{{_id}}"></i></a>
	<a data-toggle="modal" doc="{{_id}}" href='#admin-delete-modal' class="visible-xs btn btn-sm btn-danger btn-delete"><i class="fa fa-times" doc="{{_id}}"></i>{{_ "Delete"}}</a>
</template>
